
package com.right.health.app.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magics.http.parse.JsonParse;
import com.right.health.app.app.App;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.ChatMemberInfo;
import com.right.health.app.bean.ChooseMemberInfo;
import com.right.health.app.bean.ManBingInfo;

import java.util.List;


/**
 * 不区分登录用户的储存
 * 
 * @author hope
 */
public class DaoSharedPreferences {
    
    private static DaoSharedPreferences sInstance;
    
    private SharedPreferences mPreferences;
    
    private Editor mEditor;
    
    private static final String NAME = "DbConfig";
    
    private static final String USER_INFO = "user_info";

    private static final String MAN_BING_INFO = "manbing_info";

    private static final String IS_FIRST_OPEN = "is_first_open";

    private static final String CHOOSE_MEMBER = "choose:member";

    private static final String CHAT_MEMBER = "choose:member";

    private static final String IS_AGREE_PROTOCOL="is_agree_protocol";

    private DaoSharedPreferences() {
        mPreferences = App.getInstance().getApplicationContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    public static DaoSharedPreferences getInstance() {
        if (sInstance == null) {
            sInstance = new DaoSharedPreferences();
        }
        
        return sInstance;
    }

    public boolean hasCategory(String category) {
        boolean has = mPreferences.getBoolean(category, false);
        if(!has) {
            mEditor.putBoolean(category, true);
        }
        return has;
    }


    /**
     * 获取用户信息
     */
    public AppUserResponse.AppUserInfo getUser() {
        String userString = mPreferences.getString(USER_INFO, null);
        if (TextUtils.isEmpty(userString)) {
            return null;
        }

        return JsonParse.parse(userString, AppUserResponse.AppUserInfo.class);
    }

    /**
     * 设置用户信息
     * @param user 用户实体
     */
    public void setUser(AppUserResponse.AppUserInfo user) {
        String userStr;
        if(user != null){
            userStr = JsonParse.toJson(user);
        }else{
            userStr = "";
        }
        mEditor.putString(USER_INFO, userStr);
        mEditor.commit();
    }

    /**
     * 设置慢病信息
     * @param infos
     */
    public void setManBingInfo(List<ManBingInfo> infos) {
        String userStr;
        if(infos != null){
            userStr = JsonParse.toJson(infos);
        }else{
            userStr = "";
        }
        mEditor.putString(MAN_BING_INFO, userStr);
        mEditor.commit();
    }

    /**
     * 获取慢病信息
     */
    public List<ManBingInfo> getManBingInfo() {
        String str = mPreferences.getString(MAN_BING_INFO, null);
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return new Gson().fromJson(str, new TypeToken<List<ManBingInfo>>(){}.getType());
    }


    /**
     * 获取是否第一次启动程序
     */
    public boolean getIsFirstOpen(){
    	return mPreferences.getBoolean(IS_FIRST_OPEN, true);
    }
    
    /**
     * 设置是否第一次打开
     */
    public void setIsFirstOpen(Boolean isFirstOpen){
    	mEditor.putBoolean(IS_FIRST_OPEN, isFirstOpen);
        mEditor.commit();
    }

    /**
     * 获取是否同意服务隐私协议
     */
    public boolean isAgreeProtocol(){
        return mPreferences.getBoolean(IS_AGREE_PROTOCOL, false);
    }

    /**
     * 设置是否同意服务隐私协议
     */
    public void setIsAgreeProtocol(Boolean isAgree){
        mEditor.putBoolean(IS_AGREE_PROTOCOL, isAgree);
        mEditor.commit();
    }

    public void setChooseMember(ChooseMemberInfo member) {
        String userStr;
        if(member != null){
            userStr = JsonParse.toJson(member);
        }else{
            userStr = "";
        }
        mEditor.putString(CHOOSE_MEMBER, userStr);
        mEditor.commit();
    }

    public ChooseMemberInfo getChooseMember() {
        String userString = mPreferences.getString(CHOOSE_MEMBER, null);
        if (TextUtils.isEmpty(userString)) {
            return null;
        }

        return JsonParse.parse(userString, ChooseMemberInfo.class);
    }


}
